package com.calm.clibrary.andswipeback;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;

import com.calm.clibrary.R;

/**
 * Create by Calm
 * 2020/1/11 11:38
 */
public class AndSwipeBackActivity extends AppCompatActivity implements AndSwipeBackHelper.AndSlideBackManager,AndSlideActivityCallback{
    private AndSwipeBackHelper mHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(mHelper == null){
            mHelper = new AndSwipeBackHelper(this,this);
            mHelper.setOnSlideFinishListener(new AndSwipeBackHelper.AndOnSlideFinishListener() {
                @Override
                public void onFinish() {
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, R.anim.hold_on);
                }
            });
        }
    }

    @Override
    public void finish() {
        if(mHelper != null){
            mHelper.finishSwipeImmediately();
            mHelper = null;
        }
        super.finish();
    }

    /**
     * 动作拦截
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //如果自身不支持滑动返回 交由父View处理
        if(!isSupportSlideBack()){
            return super.dispatchTouchEvent(ev);
        }
        return mHelper.processTouchEvent(ev) || super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean isSupportSlideBack() {
        return true;
    }

    @Override
    public boolean canSlideBackToUs() {
        return true;
    }

    @Override
    public Activity getPreviousActivity() {
        return AndActivityLifecyleHelper.getInstance().getPreviousActivity();
    }
}
